package com.ff.device.quatz;

import com.ff.device.domain.DeviceInfo;
import com.ff.device.service.DeviceService;
import com.ff.device.util.DeviceUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

public class DeviceStatusJob extends QuartzJobBean {
	
	@Autowired
	private DeviceService deviceInfoService;

	private static final Logger logger = LoggerFactory.getLogger(DeviceStatusJob.class);
	
	@Override
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
	    List<String> deviceIpList= DeviceUtil.getDeviceList();  //通过cmd获取
	    List<DeviceInfo> localDeviceList= deviceInfoService.getDeviceList(); //从数据库中获取
        for (int i=0;i<localDeviceList.size();i++){
            DeviceInfo deviceInfo=localDeviceList.get(i);
            deviceInfo.setStatus(0);
        }
        for (int i=0;i<deviceIpList.size();i++){
            String deviceIp=deviceIpList.get(i);
            for (int j=0;j<localDeviceList.size();j++){
                DeviceInfo deviceInfo=localDeviceList.get(j);
                if(deviceInfo.getStatus()==1)
                    break;
                else if (deviceIp.contains(deviceInfo.getDeviceIp())){
                    deviceInfo.setStatus(1);
                    break;
                }
            }
        }
        //将更新结果存入数据库
        deviceInfoService.updateDevice(localDeviceList);
		logger.info("完成定时更新设备连接");
	}
}
